package usernew;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import sun.awt.geom.AreaOp.EOWindOp;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/11 8:59
 * @description:
 */
public class Usedaoio  implements  Userdao{


  @Override
  public void zhuce(User user) {

    BufferedWriter bw=null;
    try {
      bw=new BufferedWriter(new FileWriter("d:\\d.txt",true));
      bw.write(user.getId()+"_"+user.getAge()+"_"+user.getName()+"_"+user.getPassword());
      bw.newLine();
      bw.flush();

    } catch (IOException e) {
      e.printStackTrace();
    }
finally {
      if(bw!=null)
      {
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }




  }

  @Override
  public boolean login(String name, String password) {

    BufferedReader br=null;
    try {
      br=new BufferedReader(new FileReader("d:\\d.txt"));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    String line=null;
    try {
      while ((line=br.readLine())!=null)
      {
        String []data=line.split("_");
        if(data[1].equals(name)&& data[2].equals(password))
        {

          return  true;
        }

      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    return false;


  }

  @Override
  public void chaall() {

BufferedReader br=null;
    try {
      br=new BufferedReader(new FileReader("d:\\d.txt"));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    String line=null;
    try {
      while ((line=br.readLine())!=null)
      {
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

finally {
      if(br!=null)
      {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }


    }
  }

  @Override
  public void shanchu(String name, String password) {

  }

  @Override
  public void shanchu(String name) {

  }
}
